﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AppEnvironment.Web;
using DPM.Presenters;
using DPM.Dto.LoadView;
using DPM.Dto;

namespace DPM.Web.Usercontrols.Members
{
	public partial class CreateMember : UserControlWithPresenter<CreateMemberPresenter>
	{
		protected override void OnInit(EventArgs e)
		{
			Presenter.OnLoadView += new Action<Dto.LoadView.LoadCreateMemberViewDto>(Presenter_OnLoadView);
			chkCopyAddress.CheckedChanged += new EventHandler(chkCopyAddress_CheckedChanged);
			btnCreateMember.Click += new EventHandler(btnCreateMember_Click);
		}

		void btnCreateMember_Click(object sender, EventArgs e)
		{
			if (ValidatePage())
			{
				MemberDto dto = new MemberDto
				{
					Title = int.Parse(ddlTitle.SelectedValue),
					FistName = txtFirstName.Text,
					LastName = txtLastName.Text,
					IdNumber = txtIdNumber.Text,
					//DateOfBirth = Convert.ToDateTime(txtBirthDate.Text),
					DateOfBirth = DateTime.Today,
					EmailAddress = txtEmailAddress.Text,
					ContactHome = txtContactHome.Text,
					ContactCell = txtContactCell.Text,
					ContactFax = txtFax.Text,
					EmergencyContactPerson = txtEmergencyContactPerson.Text,
					ContactEmergency = txtEmergencyContact.Text,
					PostalAddress = new AddressDto
					{
						AddressLine1 = txtPostalAddressLine1.Text,
						AddressLine2 = txtPostalAddressLine2.Text,
						City = txtPostalCity.Text,
						PostalCode = txtPostalCode.Text,
						Region = int.Parse(ddlPostalRegion.SelectedValue),
						Country = "South Africa",
					},
					HomeAddress = new AddressDto
					{
						AddressLine1 = txtAddressLine1.Text,
						AddressLine2 = txtAddressLine2.Text,
						City = txtAddressCity.Text,
						PostalCode = txtAddressPostalCode.Text,
						Region = int.Parse(ddlAddressRegion.SelectedValue),
						Country = "South Africa",
					},
					CompanyName = txtCompanyName.Text,
					CompanyContactPerson = txtContactPerson.Text,
					Industry = int.Parse(ddlIndustry.SelectedValue),
					JobTitle = txtJobTitle.Text,
					ContactWork = txtWorkContact.Text,
					WorkAddress = new AddressDto
					{
						AddressLine1 = txtWorkAddressLine1.Text,
						AddressLine2 = txtWorkAddressLine2.Text,
						City = txtWorkAddressCity.Text,
						PostalCode = "",
						Region = int.Parse(ddlWorkAddressRegion.SelectedValue),
						Country = "South Africa",
					},
					Classes = GetCheckListItemIds(chklClasses),
					MemberType = int.Parse(ddlMemberType.SelectedValue),
					//ExpiryDate = Convert.ToDateTime(calExpiry.SelectedDate),
					MedicalAidProvider = txtMedicalAidProvider.Text,
					MedicalAidNumber = txtMedicalAidNumber.Text,
					DoctorName = txtDoctorName.Text,
					ContactMedical = txtMedicalAidContact.Text,
					MedicalConditions = GetCheckListItemIds(chklMedicalConditions),
					MedicalConditionDescription = txtMedicalDescription.Text,
					BankName = txtBankName.Text,
					AccountType = int.Parse(ddlAcountType.SelectedValue),
					BranchName = txtBranchName.Text,
					BranchCode = txtBranchCode.Text,
					AccountNumber = txtAccountNumber.Text,
					AccountHolder = txtAccountHolder.Text,
					AmountPayable = txtAmountPayable.Text,
					ContractType = int.Parse(ddlContractType.SelectedValue),
					MartialArtsBackground = txtMartialArtsBackground.Text,
					AdvertisementSource = GetCheckListItemIds(chklAdvertisementSource),
					Notes = GetNote(),
					Photo = flPhoto.FileName,
				};
				//TODO: Do file upload
				SubmitView(dto);
			}
		}
		void chkCopyAddress_CheckedChanged(object sender, EventArgs e)
		{
			txtAddressLine1.Text = txtPostalAddressLine1.Text;
			txtAddressLine2.Text = txtPostalAddressLine2.Text;
			txtAddressCity.Text = txtPostalCity.Text;
			foreach (ListItem item in ddlAddressRegion.Items)
			{
				if (item.Value == ddlPostalRegion.SelectedValue)
				{
					item.Selected = true;
				}
			}
			txtAddressPostalCode.Text = txtPostalCode.Text;
		}
		void Presenter_OnLoadView(LoadCreateMemberViewDto dto)
		{
			ddlMemberType.Assign(dto.MemberTypes);
			ddlTitle.Assign(dto.Titles);
			ddlContractType.Assign(dto.ContractType);
			ddlIndustry.Assign(dto.Industry);
			ddlAddressRegion.Assign(dto.Regions);
			ddlAcountType.Assign(dto.AccountType);
			ddlPostalRegion.Assign(dto.Regions);
			ddlWorkAddressRegion.Assign(dto.Regions);
			
			chklClasses.Assign(dto.Classes);
			chklAdvertisementSource.Assign(dto.AdvertisementSource);
			chklMedicalConditions.Assign(dto.MedicalConditions);
		}
		void SubmitView(MemberDto dto)
		{
			Presenter.Submit(dto);
		}
	
		private IEnumerable<int> GetCheckListItemIds(CheckBoxList chkList)
		{
			List<int> items = new List<int>();
			foreach (ListItem item in chkList.Items)
			{
				if (item.Selected)
				{
					int i = int.Parse(item.Value);
					items.Add(i);
				}
			}
			return items;
		}
		private IEnumerable<NoteDto> GetNote()
		{ 
			List<NoteDto> temp = new List<NoteDto>();
			NoteDto dto = new NoteDto
			{
				DateAdded = DateTime.Today,
				Note = txtAddNote.Text,
				User = 1, //TODO: get proper user name
			};
			temp.Add(dto);
			return temp;
		}
		private bool ValidatePage()
		{
			//TODO: do validation here
			return true;
		}
	}
}